global d_user "Your directory" //Set the directory :)
set more off

********************************************************************************
// Evaluation of Delivering Breakfast After the Bell and Academic Performance //
// among Third-Grade Children: An Application of The Synthetic Control Method //
//                         Journal of School Health                           //
//                  Cuadros-Menaca, Thomsen, and Nayga (2022)                 //
********************************************************************************

//Estimate the SCM for each school adopting BAB

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==104021
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014) 
synth ZMath $controls ZMath ZMath(2012) ZMath(2014), trunit(104021) trperiod(2015) fig nested keep(resout104021, replace)
graph save Graph "$d_user\School104021.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405034
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZMath $controls ZMath ZMath(2010) ZMath(2014), trunit(405034) trperiod(2015) fig nested keep(resout405034, replace)
graph save Graph "$d_user\School405034.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405036
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZMath $controls ZMath ZMath(2011) ZMath(2014), trunit(405036) trperiod(2015) nested fig keep(resout405036, replace)
graph save Graph "$d_user\School405036.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405040
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZMath $controls ZMath ZMath(2014), trunit(405040) trperiod(2015) fig nested keep(resout405040, replace)
graph save Graph "$d_user\School405040.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405041
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZMath $controls ZMath ZMath(2010) ZMath(2014), trunit(405041) trperiod(2015) fig nested keep(resout405041, replace)
graph save Graph "$d_user\School405041.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405043
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014) 
synth ZMath $controls ZMath ZMath(2010) ZMath(2011) ZMath(2014), trunit(405043) trperiod(2015) fig nested keep(resout405043, replace)
graph save Graph "$d_user\School405043.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405044
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014) 
synth ZMath $controls ZMath ZMath(2013) ZMath(2014), trunit(405044) trperiod(2015) fig nested keep(resout405044, replace)
graph save Graph "$d_user\School405044.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405049
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014) 
synth ZMath $controls ZMath ZMath(2012) ZMath(2014), trunit(405049) trperiod(2015) fig nested keep(resout405049, replace)
graph save Graph "$d_user\School405049.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405050
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZMath $controls ZMath ZMath(2011) ZMath(2014), trunit(405050) trperiod(2015) fig nested keep(resout405050, replace)
graph save Graph "$d_user\School405050.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==405051
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZMath $controls ZMath ZMath(2012) ZMath(2014), trunit(405051) trperiod(2015) fig nested keep(resout405051, replace)
graph save Graph "$d_user\School405051.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==701001
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014) 
synth ZMath $controls ZMath ZMath(2009) ZMath(2011) ZMath(2014), trunit(701001) trperiod(2015) fig nested keep(resout701001, replace)
graph save Graph "$d_user\School701001.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==1803025
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014) 
synth ZMath $controls ZMath ZMath(2014), trunit(1803025) trperiod(2015) fig nested keep(resout1803025, replace)
graph save Graph "$d_user\School1803025.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==1803026
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZMath $controls ZMath ZMath(2014), trunit(1803026) trperiod(2015) fig nested keep(resout1803026, replace)
graph save Graph "$d_user\School1803026.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==1803028
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014) 
synth ZMath $controls ZMath ZMath(2011) ZMath(2014), trunit(1803028) trperiod(2015) fig nested keep(resout1803028, replace )
graph save Graph "$d_user\School1803028.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==1803029
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014) 
synth ZMath $controls ZMath ZMath(2012) ZMath(2014), trunit(1803029) trperiod(2015) fig nested keep(resout1803029, replace)
graph save Graph "$d_user\School1803029.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==2303021
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014) 
synth ZMath $controls ZMath ZMath(2012) ZMath(2014), trunit(2303021) trperiod(2015) fig nested keep(resout2303021, replace)
graph save Graph "$d_user\School2303021.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==2703009
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014) 
synth ZMath $controls ZMath ZMath(2009) ZMath(2011) ZMath(2014), trunit(2703009) trperiod(2015) fig nested keep(resout2703009, replace)
graph save Graph "$d_user\School2703009.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==3211022
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014) 
synth ZMath $controls ZMath ZMath(2009) ZMath(2011) ZMath(2014), trunit(3211022) trperiod(2015) fig nested keep(resout3211022, replace)
graph save Graph "$d_user\School3211022.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==3809014
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014) 
synth ZMath $controls ZMath ZMath(2010) ZMath(2013) ZMath(2014), trunit(3809014) trperiod(2015) fig nested keep(resout3809014, replace)
graph save Graph "$d_user\School3809014.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==3810026
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014) 
synth ZMath $controls ZMath ZMath(2010) ZMath(2014), trunit(3810026) trperiod(2015) fig nested keep(resout3810026, replace)
graph save Graph "$d_user\School3810026.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==4201001
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014) 
synth ZMath $controls ZMath ZMath(2014), trunit(4201001) trperiod(2015) fig nested keep(resout4201001, replace)
graph save Graph "$d_user\School4201001.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==4712043
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014) 
synth ZMath $controls ZMath ZMath(2014), trunit(4712043) trperiod(2015) fig nested keep(resout4712043, replace)
graph save Graph "$d_user\School4712043.gph", replace

use "$d_user\Data.dta", clear
gen treatment = BAB==1 & year>=2015
egen school_out = max(treatment), by(School)
gen zero=0
replace school_out = zero if School==7203027
drop if school_out==1
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014) 
synth ZMath $controls ZMath ZMath(2012) ZMath(2014), trunit(7203027) trperiod(2015) fig nested keep(resout7203027, replace)
graph save Graph "$d_user\School7203027.gph", replace

//Graph each SC for all schools

graph combine "$d_user\School104021.gph" "$d_user\School405034.gph" /// 
              "$d_user\School405036.gph" "$d_user\School405040.gph" ///
			  "$d_user\School405041.gph" "$d_user\School405043.gph" ///
              "$d_user\School405044.gph" "$d_user\School405049.gph" ///
			  "$d_user\School405050.gph" "$d_user\School405051.gph" ///
              "$d_user\School701001.gph" "$d_user\School1803025.gph" ///
			  "$d_user\School1803026.gph" "$d_user\School1803028.gph" ///
              "$d_user\School1803029.gph" "$d_user\School2303021.gph" ///
			  "$d_user\School2703009.gph" "$d_user\School3211022.gph" ///
              "$d_user\School3809014.gph" "$d_user\School3810026.gph" ///
			  "$d_user\School4201001.gph" "$d_user\School4712043.gph" ///
              "$d_user\School7203027.gph" 

graph save Graph "$d_user\Figure 1.gph", replace			  

//Compute the effects for all the schools 

*compute the difference between the real and the SC

foreach i in 104021 405034 405036 405040 405041 405043 405044 405049 405050 405051 ///
             701001 1803025 1803026 1803028 1803029 2303021 2703009 3211022 3809014 ///
			 3810026 4201001 4712043 7203027 {
use "$d_user\resout`i'.dta", clear
keep _Y_treated _Y_synthetic _time
drop if _time>2019.
rename _Y_treated _Y_treated`i'
rename _Y_synthetic _Y_synthetic`i'

gen a =  _Y_treated`i'> _Y_synthetic`i'
gen treated_positive = _Y_treated`i'>0
gen treated_negative = _Y_treated`i'<0
gen synthetic_positive = _Y_synthetic`i' >0
gen synthetic_negative = _Y_synthetic`i' <0
gen diffa = _Y_treated`i'- _Y_synthetic`i' if a==1 & treated_positive==1 & synthetic_positive==1
gen diffb = _Y_treated`i'- _Y_synthetic`i' if a==1 & treated_positive==1 & synthetic_negative==1
gen diffc= abs(_Y_synthetic`i')-abs(_Y_treated`i') if a==1 & treated_negative==1 & synthetic_negative==1
gen diffd= _Y_treated`i'-_Y_synthetic`i' if a==0 & treated_positive==1 & synthetic_positive==1 
gen diffe= _Y_treated`i'-_Y_synthetic`i' if a==0 & treated_negative==1 & synthetic_positive==1 
gen difff= _Y_treated`i'-_Y_synthetic`i' if a==0 & treated_negative==1 & synthetic_negative==1
egen effect`i' = rowtotal(diffa diffb diffc diffd diffe difff )
save "$d_user\SCM`i'.dta", replace
}

*Put together and compute the IATE

use "$d_user\SCM104021.dta", clear 
save "$d_user\SC_effects.dta", replace

foreach i in 405034 405036 405040 405041 405043 405044 405049 405050 405051 ///
             701001 1803025 1803026 1803028 1803029 2303021 2703009 3211022 3809014 ///
			 3810026 4201001 4712043 7203027 {
use "$d_user\SC_effects.dta", clear
merge 1:1 _time using "$d_user\SCM`i'.dta"
drop _merge
save "$d_user\SC_effects.dta", replace
}

global treated _Y_treated*
egen avgtreated = rmean($treated)   
   
global synthetic _Y_synthetic*
egen avgsynthetic = rmean($synthetic) 

global effect effect*
egen effect2 =rmean($effect) 
gen effect = avgtreated-avgsynthetic
keep _time effect 
save "$d_user\Math_effects.dta", replace

//Compute the SC Effects For those schools adopting BIC 

use "$d_user\SCM104021.dta", clear 
save "$d_user\BIC_effects.dta", replace

foreach i in 405034 405036 405040 405041 405043 405044 405049 405050 405051 ///
             1803025 1803026 1803028 1803029 3810026{
use "$d_user\BIC_effects.dta", clear
merge 1:1 _time using "$d_user\SCM`i'.dta"
drop _merge
save "$d_user\BIC_effects.dta", replace
}

global treated _Y_treated*
egen avgtreated = rmean($treated)   

global synthetic _Y_synthetic*
egen avgsynthetic = rmean($synthetic) 

global effect effect*
egen effect2 =rmean($effect) 
gen effect = avgtreated-avgsynthetic
keep _time effect 
save "$d_user\BIC_effects.dta", replace

//Compute the SC Effects For those schools adopting G&G

use "$d_user\SCM2703009.dta", clear 
save "$d_user\GG_effects.dta", replace

foreach i in 3809014 4712043 3211022{
use "$d_user\GG_effects.dta", clear
merge 1:1 _time using "$d_user\SCM`i'.dta"
drop _merge
save "$d_user\GG_effects.dta", replace
}

global treated _Y_treated*
egen avgtreated = rmean($treated)   

global synthetic _Y_synthetic*
egen avgsynthetic = rmean($synthetic) 

global effect effect*
egen effect2 =rmean($effect) 
gen effect = avgtreated-avgsynthetic
keep _time effect 
save "$d_user\GG_effects.dta", replace

//Compute the SC Effects For those schools in small size districts

use "$d_user\SCM701001.dta", clear 
save "$d_user\Small_effects.dta", replace

foreach i in 2703009 3211022 3809014 7203027{
use "$d_user\Small_effects.dta", clear		
merge 1:1 _time using "$d_user\SCM`i'.dta"
drop _merge
save "$d_user\Small_effects.dta", replace
}

global treated _Y_treated*
egen avgtreated = rmean($treated)   
   
global synthetic _Y_synthetic*
egen avgsynthetic = rmean($synthetic) 

global effect effect*
egen effect2 =rmean($effect) 
gen effect = avgtreated-avgsynthetic
keep _time effect 
save "$d_user\Small_effects.dta", replace

//Compute the SC Effects For those schools in mid-size districts

use "$d_user\SCM104021.dta", clear 
save "$d_user\Mid_effects.dta", replace

foreach i in 3810026 4201001 4712043 2303021{
use "$d_user\Mid_effects.dta", clear		
merge 1:1 _time using "$d_user\SCM`i'.dta"
drop _merge
save "$d_user\Mid_effects.dta", replace
}

global treated _Y_treated*
egen avgtreated = rmean($treated)   
   
global synthetic _Y_synthetic*
egen avgsynthetic = rmean($synthetic) 

global effect effect*
egen effect2 =rmean($effect) 
gen effect = avgtreated-avgsynthetic
keep _time effect 
save "$d_user\Mid_effects.dta", replace

//Compute the SC Effects For those schools in big-size districts

use "$d_user\SCM1803025.dta", clear 
save "$d_user\Big_effects.dta", replace

foreach i in 1803026 1803028 1803029 2303021{
use "$d_user\Big_effects.dta", clear		
merge 1:1 _time using "$d_user\SCM`i'.dta"
drop _merge
save "$d_user\Big_effects.dta", replace
}

global treated _Y_treated*
egen avgtreated = rmean($treated)   
   
global synthetic _Y_synthetic*
egen avgsynthetic = rmean($synthetic) 
		
global effect effect*
egen effect2 =rmean($effect) 
gen effect = avgtreated-avgsynthetic
keep _time effect 
save "$d_user\Big_effects.dta", replace

//Compute the SC Effects For those schools in large-size districts

use "$d_user\SCM405034.dta", clear 
save "$d_user\Large_effects.dta", replace

foreach i in 405036 405040 405041 405043 405044 405049 405050 405051{
use "$d_user\Large_effects.dta", clear		
merge 1:1 _time using "$d_user\SCM`i'.dta"
drop _merge
save "$d_user\Large_effects.dta", replace
}

global treated _Y_treated*
egen avgtreated = rmean($treated)   
   
global synthetic _Y_synthetic*
egen avgsynthetic = rmean($synthetic) 
	
global effect effect*
egen effect2 =rmean($effect) 
gen effect = avgtreated-avgsynthetic
keep _time effect 
save "$d_user\Large_effects.dta", replace

//Compute P-values all sample with all schools

*Estimate the placebo SCM

foreach i in 201001 203027 401007 401009 401012 401014 401015 405033 405042	///
             405047	407026 440701 503011 503012	503013 505026 801004 803011	///
			 1201001 1203010 1304014 1305009 1408001 1408018 1503016 1505025 ///
			 1601001 1613010 1613031 1705020 1705022 1705029 1705030 1705032 ///
			 2203014 2301001 2301008 2301011 2304021 2306029 2402006 2502005 ///
			 2503009 2602005 2605033 2606042 2607046 2705023 2705024 2803016 ///
			 2807004 3102001 3301001 3302005 3509064 3510078 3510079 3510080 ///
			 3806018 4204016 4303012 4304001 4304002 4304007 4304008 4304013 ///
			 4304014 4401011 4603009 4605019 4605020 4605022 4701001 4708028 ///
			 4901001 5204025 5205028 5303010 5503010 5504014 5604015 5608035 ///
			 5801001 5804013 6001024 6001027 6001030 6001038 6001048 6003092 ///
			 6003093 6003095 6003104 6003108 6003110 6003112 6003113 6003129 ///
			 6003135 6003139 6003142 6003150 6043701 6302006 6302008 6302009 ///
			 6304029 6502005 6601001 6601002 6601003 6601006 6601007 6601008 ///
			 6601010 6601011 6601012 6601014 6601016 6601017 6601018 6601019 ///
			 6601029 6601030 6601031 6601032 6601033 6701001 6703012 6804009 ///
			 6901005 6901011 6901015 7003027 7007039 7008043 7202008 7202009 ///
			 7203012 7203013 7203016 7203017 7203022 7203023 7204027 7207044 ///
			 7207052 7207057 7207058 7207063 7207066 7208060 7301001 7307026 ///
			 7307030 7310042 7311046 7311047 7311053 7403012 7503005{
use "$d_user\Data.dta", clear
egen treated2 = total(BAB) if year>=2015, by (School)
egen unit_out2 = max(treated2), by (School)
drop if (unit_out2>0)
tsset School year	  
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZMath $controls ZMath ZMath(2012) ZMath(2014), trunit(`i') trperiod(2015) keep(Placeboresout`i', replace)
}

foreach i in 201001 203027 401007 401009 401012 401014 401015 405033 405042	///
             405047	407026 440701 503011 503012	503013 505026 801004 803011	///
			 1201001 1203010 1304014 1305009 1408001 1408018 1503016 1505025 ///
			 1601001 1613010 1613031 1705020 1705022 1705029 1705030 1705032 ///
			 2203014 2301001 2301008 2301011 2304021 2306029 2402006 2502005 ///
			 2503009 2602005 2605033 2606042 2607046 2705023 2705024 2803016 ///
			 2807004 3102001 3301001 3302005 3509064 3510078 3510079 3510080 ///
			 3806018 4204016 4303012 4304001 4304002 4304007 4304008 4304013 ///
			 4304014 4401011 4603009 4605019 4605020 4605022 4701001 4708028 ///
			 4901001 5204025 5205028 5303010 5503010 5504014 5604015 5608035 ///
			 5801001 5804013 6001024 6001027 6001030 6001038 6001048 6003092 ///
			 6003093 6003095 6003104 6003108 6003110 6003112 6003113 6003129 ///
			 6003135 6003139 6003142 6003150 6043701 6302006 6302008 6302009 ///
			 6304029 6502005 6601001 6601002 6601003 6601006 6601007 6601008 ///
			 6601010 6601011 6601012 6601014 6601016 6601017 6601018 6601019 ///
			 6601029 6601030 6601031 6601032 6601033 6701001 6703012 6804009 ///
			 6901005 6901011 6901015 7003027 7007039 7008043 7202008 7202009 ///
			 7203012 7203013 7203016 7203017 7203022 7203023 7204027 7207044 ///
			 7207052 7207057 7207058 7207063 7207066 7208060 7301001 7307026 ///
			 7307030 7310042 7311046 7311047 7311053 7403012 7503005{
use "$d_user\Placeboresout`i'.dta", clear
keep _Y_treated _Y_synthetic _time
drop if _time>2019.
rename _Y_treated _Y_treated`i'
rename _Y_synthetic _Y_synthetic`i'

gen a =  _Y_treated`i'> _Y_synthetic`i'

gen treated_positive = _Y_treated`i'>0
gen treated_negative = _Y_treated`i'<0
gen synthetic_positive = _Y_synthetic`i' >0
gen synthetic_negative = _Y_synthetic`i' <0

gen diffa = _Y_treated`i'- _Y_synthetic`i' if a==1 & treated_positive==1 & synthetic_positive==1
gen diffb = _Y_treated`i'- _Y_synthetic`i' if a==1 & treated_positive==1 & synthetic_negative==1
gen diffc= abs(_Y_synthetic`i')-abs(_Y_treated`i') if a==1 & treated_negative==1 & synthetic_negative==1

gen diffd= _Y_treated`i'-_Y_synthetic`i' if a==0 & treated_positive==1 & synthetic_positive==1 
gen diffe= _Y_treated`i'-_Y_synthetic`i' if a==0 & treated_negative==1 & synthetic_positive==1 
gen difff= _Y_treated`i'-_Y_synthetic`i' if a==0 & treated_negative==1 & synthetic_negative==1

egen placebo`i' = rowtotal(diffa diffb diffc diffd diffe difff )

keep _time placebo`i'

save "$d_user\PlaceboSCM`i'.dta", replace
}

*Put the true effects and placebo effects together

use "$d_user\Math_effects.dta", clear
save "$d_user\All_effects.dta", replace

foreach i in 201001 203027 401007 401009 401012 401014 401015 405033 405042	///
             405047	407026 440701 503011 503012	503013 505026 801004 803011	///
			 1201001 1203010 1304014 1305009 1408001 1408018 1503016 1505025 ///
			 1601001 1613010 1613031 1705020 1705022 1705029 1705030 1705032 ///
			 2203014 2301001 2301008 2301011 2304021 2306029 2402006 2502005 ///
			 2503009 2602005 2605033 2606042 2607046 2705023 2705024 2803016 ///
			 2807004 3102001 3301001 3302005 3509064 3510078 3510079 3510080 ///
			 3806018 4204016 4303012 4304001 4304002 4304007 4304008 4304013 ///
			 4304014 4401011 4603009 4605019 4605020 4605022 4701001 4708028 ///
			 4901001 5204025 5205028 5303010 5503010 5504014 5604015 5608035 ///
			 5801001 5804013 6001024 6001027 6001030 6001038 6001048 6003092 ///
			 6003093 6003095 6003104 6003108 6003110 6003112 6003113 6003129 ///
			 6003135 6003139 6003142 6003150 6043701 6302006 6302008 6302009 ///
			 6304029 6502005 6601001 6601002 6601003 6601006 6601007 6601008 ///
			 6601010 6601011 6601012 6601014 6601016 6601017 6601018 6601019 ///
			 6601029 6601030 6601031 6601032 6601033 6701001 6703012 6804009 ///
			 6901005 6901011 6901015 7003027 7007039 7008043 7202008 7202009 ///
			 7203012 7203013 7203016 7203017 7203022 7203023 7204027 7207044 ///
			 7207052 7207057 7207058 7207063 7207066 7208060 7301001 7307026 ///
			 7307030 7310042 7311046 7311047 7311053 7403012 7503005{
use "$d_user\All_effects", clear
merge 1:1 _time using "$d_user\PlaceboSCM`i'.dta"
drop _merge
keep _time effect placebo*
save "$d_user\All_effects.dta", replace
}

*Compute P-Value

use "$d_user\All_effects.dta", clear
drop if _time<2015
drop effect
reshape long placebo, i(_time) j(school) 
sort school _time

reshape wide placebo, i(school) j(_time)
drop school
save "$d_user\placebo.dta", replace

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 23 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 23 
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2019
return scalar mean = r(mean)
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear 
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

/* effect
use "$d_user\Math_effects.dta", clear
_time	effect
2015	-.16318175
2016	-.08214924
2017	-.1048955
2018	 .00530069
2019	-.01628706
*/

use "$d_user\p_values.dta", clear

histogram mean2015, tline(-.16318175) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2015 RMSPE All Units") xtitle("")
graph save Graph "$d_user\MathAllPlacebo2015.gph", replace,
histogram mean2016, tline(-.08214924) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2016 RMSPE All Units") xtitle("")
graph save Graph "$d_user\MathAllPlacebo2016.gph", replace,
histogram mean2017, tline(-.1048955) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2017 RMSPE All Units") xtitle("")
graph save Graph "$d_user\MathAllPlacebo2017.gph", replace,
histogram mean2018, tline(.00530069) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2018 RMSPE All Units") xtitle("")
graph save Graph "$d_user\MathAllPlacebo2018.gph", replace,
histogram mean2019, tline(-.01628706) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2019 RMSPE All Units") xtitle("")
graph save Graph "$d_user\MathAllPlacebo2019.gph", replace,

*Compute P-values baseline estimates all 

gen effect2015 = -.16318175
gen effect2016 = -.08214924
gen effect2017 = -.1048955
gen effect2018 =  .00530069
gen effect2019 = -.01628706
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvalues_all.dta", replace

//Compute P-values BIC with all schools

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 15 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 15 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2019 
return scalar mean = r(mean)
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019 

set seed 1234
placebo2019 
sum placebo2019

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.09187955
gen effect2016 =  .01419831
gen effect2017 = -.09581087
gen effect2018 =  .00138554
gen effect2019 = -.02557596
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesBIC_all.dta", replace

//Compute P-values G&G with all schools

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 4 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 4 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 4 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 4 
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 4 
summarize placebo2019 
return scalar mean = r(mean)
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019 

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear 
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.28275633
gen effect2016 = -.24057293
gen effect2017 = -.20837596
gen effect2018 = -.21990773
gen effect2019 = -.17101389
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesGG_all.dta", replace

//Compute P-values Big District Enrollment with all schools

*Put them together

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.1882
gen effect2016 = -.09423856
gen effect2017 = -.14560619
gen effect2018 = .07981619
gen effect2019 = .07224023
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesBigDistrict_all.dta", replace

//Compute P-values Small District Enrollment with all Schools

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13
drop _all
use "$d_user\placebo.dta", clear
bsample 5 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015

program placebo2016, rclass
version 13
drop _all 
use "$d_user\placebo.dta", clear
bsample 5 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 5 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 5 
summarize placebo2018 
return scalar mean = r(mean)
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 5 
summarize placebo2019
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear 
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.32948798
gen effect2016 = -.25540215
gen effect2017 = -.2587322
gen effect2018 = -.11943462
gen effect2019 = -.13571057
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesSmallDistrict_all.dta", replace

//Compute P-values for schools in small medium districts with all schools

*Put them together

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.18521447
gen effect2016 = -.1474192
gen effect2017 = .07497387
gen effect2018 = .1124647
gen effect2019 = .11097563
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesMidDistrict_all.dta", replace

//Compute P-values Large District Enrollment with all schools

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 9 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 9 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 9 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 9 
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 9 
summarize placebo2019 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019 

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.02278617
gen effect2016 =  .06078833
gen effect2017 = -.03394081
gen effect2018 =  .0282018
gen effect2019 = -.02904373
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesLargeDistrict_all.dta", replace

//Compute RMSPE by School in the control group

*Compute the Mean Squared Predictor Error for Placebo Estimates

use "$d_user\Data.dta", clear
egen treated2 = total(BAB) if year>=2015, by (School)
egen unit_out2 = max(treated2), by (School)
drop if (unit_out2>0)

tsset School year	  

tempname resmat
foreach i in 201001 203027 401007 401009 401012 401014 401015 405033 405042	///
             405047	407026 440701 503011 503012	503013 505026 801004 803011	///
			 1201001 1203010 1304014 1305009 1408001 1408018 1503016 1505025 ///
			 1601001 1613010 1613031 1705020 1705022 1705029 1705030 1705032 ///
			 2203014 2301001 2301008 2301011 2304021 2306029 2402006 2502005 ///
			 2503009 2602005 2605033 2606042 2607046 2705023 2705024 2803016 ///
			 2807004 3102001 3301001 3302005 3509064 3510078 3510079 3510080 ///
			 3806018 4204016 4303012 4304001 4304002 4304007 4304008 4304013 ///
			 4304014 4401011 4603009 4605019 4605020 4605022 4701001 4708028 ///
			 4901001 5204025 5205028 5303010 5503010 5504014 5604015 5608035 ///
			 5801001 5804013 6001024 6001027 6001030 6001038 6001048 6003092 ///
			 6003093 6003095 6003104 6003108 6003110 6003112 6003113 6003129 ///
			 6003135 6003139 6003142 6003150 6043701 6302006 6302008 6302009 ///
			 6304029 6502005 6601001 6601002 6601003 6601006 6601007 6601008 ///
			 6601010 6601011 6601012 6601014 6601016 6601017 6601018 6601019 ///
			 6601029 6601030 6601031 6601032 6601033 6701001 6703012 6804009 ///
			 6901005 6901011 6901015 7003027 7007039 7008043 7202008 7202009 ///
			 7203012 7203013 7203016 7203017 7203022 7203023 7204027 7207044 ///
			 7207052 7207057 7207058 7207063 7207066 7208060 7301001 7307026 ///
			 7307030 7310042 7311046 7311047 7311053 7403012 7503005{
global controls total Boy Minority Ecodis Bus SchoolPovertyIndex Teacherratio_(2013(1)2014) Class_size_(2013(1)2014) Teachers_Certified(2013(1)2014)
synth ZMath $controls ZMath ZMath(2012) ZMath(2014), trunit(`i') trperiod(2015) 
matrix `resmat' = nullmat(`resmat')\e(RMSPE)
local names `"`names' `"`i'"'"'
}
mat colnames `resmat' = "RMSPE"
mat rownames `resmat' = `names'
matlist `resmat', row("Treated Unit") 

//Compute P-values with 75<RMSPE

*Put the true effects and placebo effects together

use "$d_user\Math_effects.dta", clear
save "$d_user\All_effects.dta", replace

foreach i in 6601001 7311053 6601012 2605033 201001	4708028	5205028	401007 ///
             7202009 3510080 2602005 6601029 6001048 405047	7311047	4304001	///
			 7307030 7203022 2807004 6003092 3806018 6043701 1705030 7301001 ///
			 2301008 6901005 4304008 4304007 6003113 6003110 6003150 2705024 ///
			 5804013 405042	407026 5801001 7207044 3302005 6302008 6302009 ///
			 1705032 5504014 6804009 7203023 5204025 6001027 1305009 2705023 ///
			 6601016 505026	2607046	4204016	6001038	6003112	6001024	6601011	///
			 1705020 6003129 6601002 6701001 7203013 1613031 4304014 7207052 ///
			 2304021 6001030 7207063 3509064 7207066 401014	6601031	6601017	///
			 405033	7203016	401015 6003139 4605020 1705029 801004 5503010 ///
			 3510078 2402006 2606042 4304002 4304013 401009	6003135	6703012	///
			 2306029 401012 6304029 6601006	6601003	1613010	6302006	7203012	///
			 1203010 2301011 4901001 2803016 7207057 1408018 6601010 7003027 ///
			 7307026 6601018 7207058 440701	6601007	7310042	6003104	7208060	///
			 2301001 803011	7204027	6003108	503012 2203014 2502005 4303012 ///
			 2503009{
use "$d_user\All_effects", clear
merge 1:1 _time using "$d_user\PlaceboSCM`i'.dta"
drop _merge
keep _time effect placebo*
save "$d_user\All_effects.dta", replace
}

*Compute P-Value

use "$d_user\All_effects.dta", clear
drop if _time<2015
drop effect
reshape long placebo, i(_time) j(school) 
sort school _time

reshape wide placebo, i(school) j(_time)
drop school
save "$d_user\placebo.dta", replace

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 23 
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13 
drop _all
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2019 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019 

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

//Compute P-values baseline estimates schools <75 RMSPE

/* effect
use "$d_user\Breakfast_in_Classroom\Website\Math_effects.dta", clear
_time	effect
2015	-.16318175
2016	-.08214924
2017	-.1048955
2018	 .00530069
2019	-.01628706
*/

use "$d_user\p_values.dta", clear

histogram mean2015, tline(-.16318175) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2015 RMSPE <75") xtitle("")
graph save Graph "$d_user\75Placebo2015.gph", replace
histogram mean2016, tline(-.08214924) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2016 RMSPE <75") xtitle("")
graph save Graph "$d_user\75Placebo2016.gph", replace
histogram mean2017, tline(-.1048955) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2017 RMSPE <75") xtitle("")
graph save Graph "$d_user\75Placebo2017.gph", replace
histogram mean2018, tline(.00530069) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2018 RMSPE <75") xtitle("")
graph save Graph "$d_user\75Placebo2018.gph", replace
histogram mean2019, tline(-.01628706) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2019 RMSPE <75") xtitle("")
graph save Graph "$d_user\75Placebo2019.gph", replace

gen effect2015 = -.16318175
gen effect2016 = -.08214924
gen effect2017 = -.1048955
gen effect2018 =  .00530069
gen effect2019 = -.01628706
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvalues_75.dta", replace

//Compute P-values with RMSPE less than 50th percentile 

*Put the true effects and placebo effects together

use "$d_user\Math_effects.dta", clear
save "$d_user\All_effects.dta", replace

foreach i in 6601001 7311053 6601012 2605033 201001	4708028	5205028	401007 ///
             7202009 3510080 2602005 6601029 6001048 405047	7311047	4304001	///
			 7307030 7203022 2807004 6003092 3806018 6043701 1705030 7301001 ///
			 2301008 6901005 4304008 4304007 6003113 6003110 6003150 2705024 ///
			 5804013 405042	407026 5801001 7207044 3302005 6302008 6302009 ///
			 1705032 5504014 6804009 7203023 5204025 6001027 1305009 2705023 ///
			 6601016 505026	2607046	4204016	6001038	6003112	6001024	6601011	///
			 1705020 6003129 6601002 6701001 7203013 1613031 4304014 7207052 ///
			 2304021 6001030 7207063 3509064 7207066 401014	6601031	6601017	///
			 405033	7203016	401015 6003139 4605020 1705029 801004 5503010 ///
			 3510078 {
use "$d_user\All_effects", clear
merge 1:1 _time using "$d_user\PlaceboSCM`i'.dta"
drop _merge
keep _time effect placebo*
save "$d_user\All_effects.dta", replace
}

*Compute P-Value

use "$d_user\All_effects.dta", clear
drop if _time<2015
drop effect
reshape long placebo, i(_time) j(school) 
sort school _time

reshape wide placebo, i(school) j(_time)
drop school
save "$d_user\placebo.dta", replace

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 23 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 23 
summarize placebo2018 
return scalar mean = r(mean)
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 23 
summarize placebo2019 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019 

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

//Compute P-values baseline estimates schools <50 RMSPE

/* effect
use "$d_user\Breakfast_in_Classroom\Website\Math_effects.dta", clear
_time	effect
2015	-.16318175
2016	-.08214924
2017	-.1048955
2018	 .00530069
2019	-.01628706
*/

use "$d_user\p_values.dta", clear

histogram mean2015, tline(-.16318175) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2015 RMSPE <50") xtitle("")
graph save Graph "$d_user\50Placebo2015.gph", replace
histogram mean2016, tline(-.08214924) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2016 RMSPE <50") xtitle("")
graph save Graph "$d_user\50Placebo2016.gph", replace
histogram mean2017, tline(-.1048955) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2017 RMSPE <50") xtitle("")
graph save Graph "$d_user\50Placebo2017.gph", replace
histogram mean2018, tline(.00530069) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2018 RMSPE <50") xtitle("")
graph save Graph "$d_user\50Placebo2018.gph", replace
histogram mean2019, tline(-.01628706) graphregion(color(white)) ylabel(0(2)8) legend(off) ///
	   title("2019 RMSPE <50") xtitle("")
graph save Graph "$d_user\50Placebo2019.gph", replace

gen effect2015 = -.16318175
gen effect2016 = -.08214924
gen effect2017 = -.1048955
gen effect2018 =  .00530069
gen effect2019 = -.01628706
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvalues_50.dta", replace

//Compute P-values BIC schools 50< RMSPE

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 15 
summarize placebo2019 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019 

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.09187955
gen effect2016 =  .01419831
gen effect2017 = -.09581087
gen effect2018 =  .00138554
gen effect2019 = -.02557596
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesBIC_50.dta", replace

//Compute P-values G&G schools 50< RMSPE

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 4 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13
drop _all 
use "$d_user\placebo.dta", clear
bsample 4 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 4 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 4 
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13
drop _all 
use "$d_user\placebo.dta", clear
bsample 4 
summarize placebo2019 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019 

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear 
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.28275633
gen effect2016 = -.24057293
gen effect2017 = -.20837596
gen effect2018 = -.21990773
gen effect2019 = -.17101389
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesGG_50.dta", replace

//Compute P-values Big District Enrollment 50< RMSPE

*Put them together

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.1882
gen effect2016 = -.09423856
gen effect2017 = -.14560619
gen effect2018 = .07981619
gen effect2019 = .07224023
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesBigDistrict_50.dta", replace

//Compute P-values Small District Enrollment

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 5 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 5 
summarize placebo2016 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 5 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 5 
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 5 
summarize placebo2019 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019 

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.32948798
gen effect2016 = -.25540215
gen effect2017 = -.2587322
gen effect2018 = -.11943462
gen effect2019 = -.13571057
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesSmallDistrict_50.dta", replace

//Compute P-values for schools in small medium districts

*Put them together

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.18521447
gen effect2016 = -.1474192
gen effect2017 = .07497387
gen effect2018 = .1124647
gen effect2019 = .11097563
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesMidDistrict_50.dta", replace

//Compute P-values Large District Enrollment

program drop placebo2015 placebo2016 placebo2017 placebo2018 placebo2019

program placebo2015, rclass 
version 13 
drop _all
use "$d_user\placebo.dta", clear 
bsample 9 
summarize placebo2015 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2015=r(mean), reps(1000000) saving(sim2015, replace) nodots: placebo2015 

program placebo2016, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear
bsample 9 
summarize placebo2016 
return scalar mean = r(mean)
end
set seed 1234
simulate mean2016=r(mean), reps(1000000) saving(sim2016, replace) nodots: placebo2016 

program placebo2017, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 9 
summarize placebo2017 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2017=r(mean), reps(1000000) saving(sim2017, replace) nodots: placebo2017 

program placebo2018, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 9 
summarize placebo2018 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2018=r(mean), reps(1000000) saving(sim2018, replace) nodots: placebo2018 

program placebo2019, rclass 
version 13 
drop _all 
use "$d_user\placebo.dta", clear 
bsample 9 
summarize placebo2019 
return scalar mean = r(mean) 
end
set seed 1234
simulate mean2019=r(mean), reps(1000000) saving(sim2019, replace) nodots: placebo2019 

set seed 1234
placebo2019 
sum placebo2019

*Put them together

foreach i in 2015 2016 2017 2018 2019{
use "$d_user\sim`i'", clear 
gen i= _n
save "$d_user\sim`i'.dta", replace
}

use "$d_user\sim2015.dta", clear
save "$d_user\p_values.dta", replace

foreach i in 2016 2017 2018 2019{
use "$d_user\p_values.dta", clear
merge 1:1 i using "$d_user\sim`i'.dta"
drop _merge
save "$d_user\p_values.dta", replace
}

use "$d_user\p_values.dta", clear

gen effect2015 = -.02278617
gen effect2016 =  .06078833
gen effect2017 = -.03394081
gen effect2018 =  .0282018
gen effect2019 = -.02904373
 
*two-sided p-values

gen z=1
gen higher15 = 1 if abs(mean2015)>abs(effect2015)
gen higher16 = 1 if abs(mean2016)>abs(effect2016)
gen higher17 = 1 if abs(mean2017)>abs(effect2017)
gen higher18 = 1 if abs(mean2018)>abs(effect2018)
gen higher19 = 1 if abs(mean2019)>abs(effect2019)

egen v_15= total(higher15), by (z)
egen v_16= total(higher16), by (z)
egen v_17= total(higher17), by (z)
egen v_18= total(higher18), by (z)
egen v_19= total(higher19), by (z)

gen p2_value2015=v_15/1000000
gen p2_value2016=v_16/1000000
gen p2_value2017=v_17/1000000
gen p2_value2018=v_18/1000000
gen p2_value2019=v_19/1000000

bys z : keep if _n==1 
keep p2_*
save "$d_user\pvaluesLargeDistrict_50.dta", replace

